<?php
$sanitize_all_escapes = true;
$fake_register_globals = false;
if (!isset($_REQUEST['medico'])) 
    session_start();

$landingpage = "index.php?site=" . $_SESSION['site_id'];
if (isset($_SESSION['pid']) && isset($_SESSION['patient_portal_onsite'])) {
    $pid = $_SESSION['pid'];
} else {
    if (!isset($_REQUEST['medico'])) {
        session_destroy();
        header('Location: ' . $landingpage . '&w');
        exit;
    } 
}
$ignoreAuth = true;
include_once('../interface/globals.php');

if (isset($_REQUEST['medico']))
    $pid = $_REQUEST['set_pid'];
$medico = "";
$medicoIni = "";
if (isset($_REQUEST['medico'])) {
    $medico = "&medico=1&set_pid=".$pid;
    $medicoIni = "?medico=1&set_pid=".$pid;
}

$special_timeout = 3600;

$formname = $_GET["formname"];

//echo "formname=> ". $formname;

$is_lbf = substr($formname, 0, 3) === 'LBF';

if ($is_lbf) {
    // Determine the default field ID and its title for graphing.
    // This is from the last graphable field in the form.
    $default = sqlQuery("SELECT field_id, title FROM layout_options WHERE " .
            "form_id = ? AND uor > 0 AND edit_options LIKE '%G%' " .
            "ORDER BY group_name DESC, seq DESC, title DESC LIMIT 1", array($formname));
}

//Bring in the style sheet
?>
<?php
// Hide the current value css entries. This is currently specific
//  for the vitals form but could use this mechanism for other
//  forms.
// Hiding classes:
//  currentvalues - input boxes
//  valuesunfocus - input boxes that are auto-calculated
//  editonly      - the edit and cancel buttons
// Showing class:
//  readonly      - the link back to summary screen
// Also customize the 'graph' class to look like links.
?>

<style>
    .currentvalues { display: none;}
    .valuesunfocus { display: none;}
    .editonly      { display: none;}

    .graph {color:#43ACA4;}

    #chart {
        border-style:solid;
        border-width:2px;
        display: block;
        margin: 0 auto;
    }
</style>

<script type="text/javascript" src="<?php echo $web_root; ?>/library/dialog.js"></script>
<script type="text/javascript" src="<?php echo $web_root; ?>/library/js/jquery-1.10.0.min.js"></script>
<script type="text/javascript" src="<?php echo $web_root; ?>/library/js/jquery-migrate-1.2.1.min.js"></script>
<script type="text/javascript" src="<?php echo $web_root; ?>/library/js/jquery.tools.min.js"></script>
<script type="text/javascript" src="<?php echo $web_root; ?>/library/js/common.js"></script>
<script type="text/javascript" src="<?php echo $web_root; ?>/patients/js/ajtooltip.js"></script>
<!-- end -->

<link rel="stylesheet" href="css/base.css" type="text/css"/>
<link rel="stylesheet" href="css/nightingale.css" type="text/css"/>

<script type="text/javascript" src="<?php echo $web_root; ?>/library/openflashchart/js/json/json2.js"></script>
<script type="text/javascript" src="<?php echo $web_root; ?>/library/openflashchart/js/swfobject.js"></script>
<script type="text/javascript">

    // variable that holds graph information for the open_flash_chart_data() function
    var data;

    // Function that is built into swfobject.js script that will collect the
    //   data used for the graph.
    function open_flash_chart_data()
    {
        return JSON.stringify(data);
    }
	
    // Show the selected chart in the 'chart' div element
    function show_graph(table_graph, name_graph, title_graph)
    {
        $.ajax({ url: '<?php echo $web_root; ?>/patients/lib_graphs_portal.php<?php echo $medicoIni; ?>',
            type: 'POST',
            data: ({ table: table_graph,
                name: name_graph,
                title: title_graph
            }),
            dataType: "json",
            success: function(returnData){
                // place the raw graph data in the data variable
                data=returnData;
                // this function will automatically call open_flash_chart_data() in order to collect the raw data
                swfobject.embedSWF('<?php echo $web_root; ?>/library/openflashchart/open-flash-chart.swf', "chart", "700", "200", "9.0.0");
                // ensure show the chart div
                $('#chart').show();
            },
            error: function() {
                // hide the chart div
                $('#chart').hide();
            }	
        });
    }

    $(document).ready(function(){

        // Use jquery to show the 'readonly' class entries
        $('.readonly').show();

        // Place click callback for graphing
        $(".graph").click(function(e){ show_graph('<?php echo $is_lbf ? $formname : 'form_vitals'; ?>', this.id, $(this).text()) });

        // Show hovering effects for the .graph links
        $(".graph").hover(
        function(){
            $(this).css({color:'#00a400'}); //mouseover
        },
        function(){
            $(this).css({color:'#43ACA4'}); // mouseout
        }
    );

        // show blood pressure graph by default
<?php if ($is_lbf) { ?>
            show_graph('<?php echo $formname; ?>','<?php echo $default['field_id']; ?>','<?php echo $default['title']; ?>');
<?php } else { ?>
            /*ENTRA AQUI*/
            show_graph('form_vitals','weight_metric','Peso');
<?php } ?>
    });
</script>

<?php
if ($is_lbf) {
    //echo "1111111111";
    // Use the List Based Forms engine for all LBFxxxxx forms.
    include_once("$incdir/forms/LBF/new.php");
} else {
    //echo "2222222";
    // ensure the path variable has no illegal characters
    check_file_dir_name($formname);
    //plantilla de datos
    include_once("$incdir/forms/$formname/new.php");
//    echo "$incdir/forms/$formname/new.php";
}
//echo $web_root."<br/>".$GLOBALS['srcdir']. "/openflashchart/php-ofc-library/open-flash-chart.php"."<br/>".dirname(__FILE__). "/../../interface/globals.php";
?>